<?php
namespace Api\Controller;
use Think\Controller;
class InfoController extends CommonController {
    public function index(){
        $map['tp_info.wid'] = session('wid');
        $map['tp_info.status'] = 1;
        $map['tp_info.cid'] = $_GET['cid'];
        $list = M("Info")->join('tp_member ON tp_info.openid = tp_member.openid')
                         ->join('tp_cate on tp_cate.id = tp_info.cid')
                         ->where($map)
                         ->field("tp_info.*, tp_member.nickname, tp_member.headimgurl,tp_cate.title as ctitle")
                         ->order("last_time desc")
                         ->select();
        if (!$list) {
            $row_cate = [];
            $row = M("cate")->where(['id' => $_GET['cid']])->find();
            $row_cate[] = ['ctitle' => $row['title']];
            $this->ajaxReturn($row_cate);
        }
        foreach ($list as $key => $value) {
            if($value['is_top'] == 1 && $value['top_end_time'] < date("Y-m-d H:i:s")){
                $list['new'][$key]['is_top'] = 0;
            }
            $list[$key]['tags'] = explode(',', $value['tag']);
            $list[$key]['imgurl'] = explode(',', trim($value['pics'], ','))[0];
        }
        $this->ajaxReturn($list);
    }

    //搜索
    public function search(){
        $wid = session('wid');
        $k = $_GET['k'];
        $map['title']=array('like','%'.$k.'%');
        $map['status']=1;
        $list = M("Info")->field("tp_info.*, tp_member.nickname, tp_member.headimgurl")->join('tp_member ON tp_info.openid = tp_member.openid')
                         ->where($map)
                         ->where(['tp_info.wid' => $wid])
                         ->order("create_time desc")
                         ->select();

        foreach($list as $k=>$v){
            $list[$k]['url']='/pages/index/common/detail';
            $list[$k]['cate']='同城信息';
        }
        $list1 = M("House")->field("tp_house.*, tp_member.nickname, tp_member.headimgurl")->join('tp_member ON tp_house.openid = tp_member.openid')
                           ->where($map)
                           ->where(['tp_house.wid' => $wid])
                           ->order("create_time desc")
                           ->select();
        foreach($list1 as $k=>$v){
            $list1[$k]['url']='/pages/index/house/detail';
            $list1[$k]['cate']='房产';
        }
        $list= array_merge($list,$list1);
        $list2 = M("Job")->field("tp_job.*, tp_member.nickname, tp_member.headimgurl")->join('tp_member ON tp_job.openid = tp_member.openid')
                         ->where($map)
                         ->where(['tp_job.wid' => $wid])
                         ->order("create_time desc")
                         ->select();

        foreach($list2 as $k=>$v){
            $list2[$k]['url']='/pages/index/job/detail';
            $list2[$k]['cate']='招聘';
        }

        $list = array_merge($list,$list2);

        $map1['a.name']=array('like','%'.$_GET['k'].'%');
        $map1['a.status']=1;
        //$list3 = M("Shop")->where($map1)->order("create_time desc")->select();
//        foreach ($map1 as $key => $value) {
//            $map1['a.'. $key] = $value;
//            unset($map1[$key]);
//        }
        $list3 = M("Shop")->alias('a')
                           ->field('a.*, b.nickname, b.headimgurl')
                           ->join('tp_member b ON a.openid = b.openid')
                           ->where($map1)
                           ->where(['a.wid' => $wid])
                           ->where(['a.join_end_time' => ['gt', date('Y-m-d H:i:s', time())]])
                           ->order("last_time desc")
                           ->select();
        //
        foreach($list3 as $k=>$v){
            //url
            switch($v['type']){
                case '':
                    $list3[$k]['url']='/pages/shop/detail';
                    break;
                case 'car':
                    $list3[$k]['url']='/pages/shop/car/detail';
                    break;
                case 'jiaoyu':
                    $list3[$k]['url']='/pages/shop/jiaoyu/detail';
                    break;
                case 'jiancai':
                    $list3[$k]['url']='/pages/shop/jiancai/detail';
                    break;
                default :
                    $list3[$k]['url']='/pages/shop/detail';
            }

          $list3[$k]['imgurl']=$list3[$k]['logo_url'];
          $list3[$k]['id']=$list3[$k]['shop_id'];
        }
         $list = array_merge($list,$list3);
        //$list4 = M("Goods")->where($map)->order("create_time desc")->select();
        foreach ($map as $key => $value) {
            $map['a.'. $key] = $value;
            unset($map[$key]);
        }
        $list4 = M("Goods")->alias('a')
                            ->field('a.*, b.join_end_time, b.cate, tp_member.nickname, tp_member.headimgurl')
                            ->join('tp_shop b on a.shop_id = b.shop_id')
                            ->join('tp_member ON a.openid = tp_member.openid')
                            ->where($map)
                            ->where(['a.wid' => $wid])
                            ->where(['b.join_end_time' => ['gt', date('Y-m-d H:i:s', time())]])
                            ->order("last_time desc")
                            ->select();
        //
        foreach($list4 as $k=>$v){
            $list4[$k]['url']='/pages/shop/goods/detail';
            $list4[$k]['imgurl']=$list4[$k]['img_url'];
        }
         $list = array_merge($list,$list4);

        $map5['a.name']=array('like','%'.$_GET['k'].'%');
        $map5['a.status']=2;

        $list5 = M("Shop")->alias('a')
            ->field('a.*')
            ->where($map5)
            ->where(['a.wid' => $wid])
            //->where(['a.join_end_time' => ['gt', date('Y-m-d H:i:s', time())]])
            ->order("last_time desc")
            ->select();

        foreach($list5 as $k=>$v){
            $list5[$k]['url']='/pages/shop/detail';
            $list5[$k]['imgurl']=$list5[$k]['logo_url'];
            $list5[$k]['id']=$list5[$k]['shop_id'];
        }
        $list = array_merge($list,$list5);

        foreach($list as $key=>$v){
        $list[$key]['sort_time'] = date("YmdHis",strtotime($v['create_time']));
        } 
        $datetime = array();
        foreach ($list as $user) {
        $datetime[] = $user['sort_time'];
        }
       
        array_multisort($datetime,SORT_DESC,$list);



        $this->ajaxReturn($list);
        exit;
    }
    public function shopSearch()
    {
        if (isset($_GET['pageNum'])) {
            $page = I('pageNum');
            $page = (int)$page * 10;
        } else {
            $page = 0;
        }

        $map = array();

        // 需要判断分类是一级分类还是二级分类
       if(intval(I('cid'))>0) {
           //$map['cid'] = $_GET['cid'];
            $cid = I('cid');
            $cate = M("Cate")->where("id=$cid")->find();

        //$map['join_end_time'] = array('<=',date('Y-m-d H:i:s',time()));
            if ($cate['pid']) {
                $map['cid'] = I('cid');
            } else {
                // 是一级分类的时候
                $map['_string'] = "cid in (select id from tp_cate where pid = $cid)";
                //$map['cid'] = 0;
            }
        }
        if(I('aid') !=""){
            $where['id'] = I('aid');
            if($where['id'] != 0){
                //不是选择全辖区
                $city = M('city')->where($where)->find();
            }else{
                //选择全辖区
                $map2['wid'] = session('wid');
                $wechat = M("Wechat")->where($map2)->find();
                $where2['city_code'] = $wechat['city'];
                $city = M("City")->where($where2)->find();
            }
            $map['county_name | site_name'] = ['like',"%".$city['city_name']."%"];
        }

        if ($_GET['type']) {
            $map['type'] = $_GET['type'];
        }
        if(!empty($_GET['k'])){
            $map['name'] = ['like',"%".$_GET['k']."%"];
        }
        $map['wid'] = session('wid');
        $map['status'] = [['eq',1],['eq',2],'or'];
        //$list = M("Shop")->where($map)->order("last_time desc")->select();
        $list = M("shop")->where($map)
                          ->where(['join_end_time' => ['gt', date('Y-m-d H:i:s', time())]])
                          ->order("last_time desc")
                          ->limit(0, $page)
                          ->select();
        //$this->ajaxReturn($list);
        
        foreach ($list as $k => $v) {
            //url
            switch($v['type']){
                case '':
                    $list[$k]['url']='/pages/shop/detail';
                    break;
                case 'car':
                    $list[$k]['url']='/pages/shop/car/detail';
                    break;
                case 'jiaoyu':
                    $list[$k]['url']='/pages/shop/jiaoyu/detail';
                    break;
                case 'jiancai':
                    $list[$k]['url']='/pages/shop/jiancai/detail';
                    break;
                default :
                    $list[$k]['url']='/pages/shop/detail';
            }
            //id
            $list[$k]['id']=$v['shop_id'];

            $eq['shop_id'] = $v['shop_id'];
            $eq['is_pin'] = 1;
            $eq['status'] = 1;
            $list[$k]['has_pin'] = M("Goods")->where($eq)->find();

            $eq1['shop_id'] = $v['shop_id'];
            $eq1['is_pin'] = 0;
            $eq1['is_kan'] = 1;
            $eq1['status'] = 1;
            $list[$k]['has_kan'] = M("Goods")->where($eq1)->find();

            $eq2['shop_id'] = $v['shop_id'];
            $eq2['is_qiang'] = 1;
            $eq2['status'] = 1;
            $list[$k]['has_qiang'] = M("Goods")->where($eq2)->find();

            $eq3['shop_id'] = $v['shop_id'];
            $eq3['status'] = 1;
            // $eq3['is_qiang'] = 1;
            $list[$k]['has_coupon'] = M("Coupon")->where($eq3)->find();
        }
        $this->ajaxReturn($list);
    }

    public function item(){
        $map['tp_info.wid'] = session('wid');
        $map['tp_info.status'] = 1;
        $map['tp_info.id'] = $_GET['id'];
        $info = M("Info")->join('tp_member ON tp_info.openid = tp_member.openid')->where($map)->field("tp_info.*, tp_member.nickname, tp_member.headimgurl")->find();

        $info['pics'] = explode(",", trim($info['pics'], ','));
        $info['tags'] = explode(",", $info['tag']);
        $this->ajaxReturn($info);
    }

    public function add(){
        foreach ($_POST as $key => $value) {
            if(!is_numeric($value) && empty($value)){
                $arr = array("err_code"=>400,"err_msg"=>"请填写全部信息！");
                $this->ajaxReturn($arr);
            }
        }
        $_POST['wid'] = session('wid');
        $_POST['create_time'] = date("Y-m-d H:i:s");

        $_POST['cid'] = $_GET['type'];
        $_POST['sub_cid'] = $_GET['sub_type'];
        $_POST['status'] = 0;

        if($_GET['sub_type'] != 0){
            $eq['id'] = $_GET['sub_type'];
        }else{
            $eq['id'] = $_GET['type'];
        }
        $_POST['cate'] = M("Cate")->where($eq)->getField('title');
        if($info_id = M("Info")->add($_POST)){
            $this->getInfoOrder($info_id);

            $map['id'] = $info_id;
            $data['status'] = 1;
            M("Info")->where($map)->save($data);
            $arr = array("err_code"=>0, "err_msg"=>"发布成功！");
            $this->ajaxReturn($arr);
        }else{
            $arr = array("err_code"=>400,"err_msg"=>"网络忙，请稍后再试！");
            $this->ajaxReturn($arr);
        }

    }

    public function refresh(){
        $eq['wid'] = session('wid');
        $eq['info_id'] = $_GET['id'];
        $eq['action'] = $_GET['action'];
        $map = $eq;
        $map['type'] = 'refresh';
        $map['date'] = date("Y-m-d");
        if(M("InfoLog")->where($map)->find()){
            $arr = array("err_code"=>400,"err_msg"=>"今天已经刷新过了，明天再来吧！");
            $this->ajaxReturn($arr);
        }else{
            M("InfoOrder")->where($eq)->setField('last_refresh_time', date("Y-m-d H:i:s"));
            M("InfoLog")->add($map);
            $arr = array("err_code"=>0, "err_msg"=>"刷新成功！");
            $this->ajaxReturn($arr);
        }
    }
    public function topInfo(){
        $list = M('top')->where(array('wid'=>session('wid')))->getField('title',true);
        $this->ajaxReturn($list);
    }
    //设置置顶
    public function setTop(){
        $id = $_POST['id'];
        $action= $_POST['action'];
        $openid = $_POST['openid'];
        $title = $_POST['title'];
        $this->setTopInfo($id,$action,$openid,$title);

    }
    public function top(){
        switch ($_GET['action']) {
            case 'house':
                $model = M("House");
                break;
            case 'job':
                $model = M("Job");
                break;
            case 'pinche':
                $model = M("Pinche");
                break;
            case 'info':
                $model = M("Info");
                break;
        }
        $map['wid'] = session('wid');
        $map['id'] = $_GET['id'];

        $model->where($map)->setField('is_top',1);

        $eq['wid'] = session('wid');
        $eq['info_id'] = $_GET['id'];
        M("InfoOrder")->where($eq)->setField('is_top',1);
    }

    public function del(){

        switch ($_GET['action']) {
            case 'house':
                $model = M("House");
                break;
            case 'job':
                $model = M("Job");
                break;
            case 'pinche':
                $model = M("Pinche");
                break;
            case 'info':
                $model = M("Info");
                break;
        }
        $omap = array();
        $omap['action'] = $_GET['action'];
        $omap['info_id'] = $_GET['id'];
        $map['wid'] = session('wid');
        $map['id'] = $_GET['id'];
        $data = array();
        if($model->where($map)->delete() && M('info_order')->where($omap)->delete()){
            $data['err_code'] = 0;
            $data['err_msg'] = "删除成功！";
            $this->ajaxReturn($data);
//            $this->success('删除成功！');
        }else{
            $data['err_code'] = 400;
            $data['err_msg'] = "网络忙，请稍后再试！";
//            $this->error('网络忙，请稍后再试！');
            $this->ajaxReturn($data);
        }
    }
}